package com.yzt.dynamicmodule.dialog;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.yzt.p_base.utils.NetworkUtils;
import com.yzt.p_base.utils.ToastUtil;
import com.yzt.redpack.GameTransHostActivity;
import com.yzt.zhuju.R;
import com.yzt.zhuju.dialog.BottomBaseDialog;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.game.GamePunishPriceBean;
import com.yzt.zhuju.network.entity.mine.wallet.WalletBean;
import com.yzt.zhuju.utils.FastClickUtils;
import com.yzt.zhuju.utils.RechargeUtil;
import com.yzt.zhuju.widget.widget.ImageLoadManger;
import com.yzt.zhuju.widget.widget.RCImageView;

/**
 * 惩罚游戏
 */
public class GamePunishDialog extends BottomBaseDialog {

    private TextView tvDiamonds;

    private String userId;

    private String gameId;

    private RCImageView imgAvatar;

    private TextView tv_user_name;

    private TextView tv_punish_price;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_game_punish;
    }

    @Override
    public void bindView(View v) {
        if (getArguments() == null) {
            return;
        }
        gameId = getArguments().getString("extra_game_id");
        initViewClick();
        iniDiamondData();
    }

    private void initViewClick() {

        imgAvatar = findViewById(R.id.img_avatar);
        tv_user_name = findViewById(R.id.tv_user_name);
        tvDiamonds = findViewById(R.id.tv_diamonds);
        tv_punish_price = findViewById(R.id.tv_punish_price);

        // 充值
        findViewById(R.id.tv_top_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RechargeUtil.toRecharge(getActivity(), false);
            }
        });

        // 发送惩罚
        findViewById(R.id.tv_gift_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendGift();
            }
        });

        // 关闭
        findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });

        // 送给
        findViewById(R.id.ll_send_contact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), GameTransHostActivity.class);
                intent.putExtra("extra_type", 2);
                intent.putExtra("extra_id", gameId);
                startActivityForResult(intent, 2);
            }
        });
    }

    /**
     * 初始化你的钻石
     */
    private void iniDiamondData() {
        BizLayer.getInstance().getMineModule().wallet(new AresSubscriber<WalletBean>() {
            @Override
            protected void onSuccess(WalletBean walletEntry) {
                tvDiamonds.setText(walletEntry.getCandyNum() + " 糖币");
            }
        });

        BizLayer.getInstance().getMineModule().punishPrice(new AresSubscriber<GamePunishPriceBean>() {
            @Override
            protected void onSuccess(GamePunishPriceBean bean) {
                tv_punish_price.setText(bean.getPunishCardPrice() + " 糖币");
            }
        });
    }

    /**
     * 发送惩罚
     */
    private void sendGift() {
        //发送
        if (FastClickUtils.isFastClick()) {
            return;
        }

        if (!NetworkUtils.isConnected()) {
            ToastUtil.showMessage(getString(R.string.rc_network_exception));
            return;
        }

        if (userId == null) {
            ToastUtil.showMessage("请选择惩罚对象");
            return;
        }

        JSONObject json = new JSONObject();
        json.put("userId", userId);
        json.put("activityId", gameId);

        BizLayer.getInstance().getGiftModule().postPunishToOther(json, new AresSubscriber<Object>() {
            @Override
            protected void onSuccess(Object response) {
                ToastUtil.showMessage("发送惩罚");
                iniDiamondData();
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (code == 934) {
                    RechargeUtil.toRecharge(getActivity(), false);
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == RESULT_OK) {
            userId = data.getStringExtra("extra_data");
            String avatar = data.getStringExtra("extra_avatar");
            ImageLoadManger.loadIv(getContext(), avatar, imgAvatar);
            String userName = data.getStringExtra("extra_user_name");
            tv_user_name.setText(userName);
            imgAvatar.setVisibility(View.VISIBLE);
            tv_user_name.setVisibility(View.VISIBLE);
        }
    }
}
